package io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class IODemo7 {
    public static void main(String[] args) throws IOException{
        try(InputStream inputStream = new FileInputStream("./test.txt")) {
            //进行文件操作
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    //文件读取完毕
                    break;
                }
                //打印这个字节的数据
                String s = new String(buffer,0,n);
                System.out.print(s);
            }
        }
    }

    public static void main2(String[] args) throws IOException{
        try(InputStream inputStream = new FileInputStream("./test.txt")) {
            //进行文件操作
            while(true) {
                int n = inputStream.read();
                if(n == -1) {
                    //文件读取完毕
                    break;
                }
                //打印这个字节的数据
                System.out.printf("%x ",n);
            }
        }
    }


    public static void main1(String[] args) throws IOException {
        InputStream inputStream = null;
        try {
            //打开文件
            inputStream = new FileInputStream("./test.txt");
            //进行文件操作
        } finally {
            //关闭文件
            inputStream.close();
        }

        try {
            // 可能会发生异常的代码
        } catch (Exception e) {
            // 异常处理
        } finally {
            // 无论是否发生异常，都会执行的代码
        }
    }
}
